function [A,data]=FreqFindANoise_new()
% Fits data to a linear curve to find the white noise level, A, for use in
% findQ.m
% 
% Data file must be semicolon delimited, with a 3-line heading (NOVA export as .txt).
% 
% OUTPUT:   Qf, Quality factor
%           wf, resonance freq, kHz 
%           Qstats = [A B wf Qf]

[afile,path] = uigetfile('.txt','Select file'); %file extension, title of window

raw = importdata(strcat(path,afile),';',3);              %tab delimited
data = raw.data;

%Trim?

h(1) = plot(data(:,1),data(:,2),'b-');
userq = 'No';

while ~strcmp(userq,'Yes') %Trim
    userq = questdlg('Trim?');
    if ~strcmp(userq,'Yes')
        break
    end
    disp('Select start and end of trim.')
    ends = ginput(2); %Select start and end
    ind1 = find(data(:,1)>ends(1,1),1); %Find start index
    ind2 = find(data(:,1)<ends(2,1),1,'last'); %Find end index

    data1 = data(1:ind1,:);
    data2 = data(ind2:end,:);
    data = vertcat(data1,data2);

    delete(h(1))
    h(1) = plot(data(:,1),data(:,2),'b-');  
end
set(h(1),'displayname','Data')

x = data(:,1);
y = data(:,2);

A = polyfit(x,y,0);


% Plot fit

hold on
h(2) = plot(x,ones(length(x),1).*A,'k--');
set(h(2),'linewidth',0.5,'displayname','Fit')
xlabel('Frequency (kHz)')
ylabel('Amplitude (nA)')
legend(h)
hold off
